#!/usr/bin/python
# plot_SimpleIntegrateCluster.py

import math
import numpy
from matplotlib import use as useBackend
useBackend('Agg')
import matplotlib.pyplot as ppt


def cheat111():
    '''Cheat to clear the plot.'''
    ppt.subplot(311)
    ppt.plot([0,1],[1,2])
    ppt.subplot(131)
    ppt.plot([0,1],[1,2])
    ppt.subplot(111)
    ppt.hold(True)


colorlist = ['k', 'b', 'g', 'r', 'c', 'm', 'y', 'indigo', 'darkorange', 'saddlebrown', 'forestgreen', 'deeppink', 'slategray', 'maroon', 'lime', 'bisque', 'aquamarine', 'darkcyan', 'darkmagenta', 'mediumpurple', 'olive', 'purple', 'salmon'] #23


def choosecolor(j, colorlist):
    ncolors = len(colorlist)
    while True:
        if j > (ncolors - 1):
            j = j - ncolors
        else:
            colornow = colorlist[j]
            break
    return colornow


def plot(simpars, phypars, bHistTuple):
    '''Shows GM_SimpleIntegrate's results.'''
    for i in simpars:
        cmd = "%s = simpars['%s']" % (i,i)
        exec cmd
    for i in phypars:
        cmd = "%s = phypars['%s']" % (i,i)
        exec cmd
    (bHistCumulative, CumulativeAngle, bHistCumulativeAreaNew, CumulativeAreaNew) = bHistTuple
    cbshrink = 0.8
    aspectshrink = 0.8
    aspectshrink2 = 0.6

    cheat111()
    anglelims = [CumulativeAngle[0], CumulativeAngle[-1], MvVals[0], MvVals[-1]]
    width = math.fabs(CumulativeAngle[0] - CumulativeAngle[-1])
    height = math.fabs(MvVals[0] - MvVals[-1])
    aratio = aspectshrink*width/height
    ppt.imshow(bHistCumulative, origin='lower', extent=anglelims, aspect=aratio)
    ppt.colorbar(shrink=cbshrink)
    ppt.contour(bHistCumulativeAreaNew, origin='lower', extent=anglelims, aspect=aratio)
    ppt.title("Lower limit survey yield chart")
    ppt.xlabel("Angle covered from galactic zenith")
    ppt.ylabel("Limiting unlensed source apparent magnitude")
    plotname = outdir+"DegLatitudeHistogramPlot"+timestamp+".pdf"
    ppt.savefig(plotname)

    cheat111()
    anglelims = [1e-3*CumulativeAreaNew[0], 1e-3*CumulativeAreaNew[-1], MvVals[0], MvVals[-1]]
    width = math.fabs(1e-3*CumulativeAreaNew[0] - 1e-3*CumulativeAreaNew[-1])
    height = math.fabs(MvVals[0] - MvVals[-1])
    aratio = aspectshrink*width/height
    ppt.imshow(bHistCumulativeAreaNew, origin='lower', extent=anglelims, aspect=aratio)
    ppt.colorbar(shrink=cbshrink)
    conseq = [1e-3,1e-2,1e-1,1,10,1e2,1e3]
    ctrs = ppt.contour(bHistCumulativeAreaNew, conseq, origin='lower', extent=anglelims, aspect=aratio)
    ppt.clabel(ctrs)
    ppt.title("Lower limit survey yield chart")
    ppt.xlabel("Angular area in 1000 deg$^2$ covered from galactic zenith")
    ppt.ylabel("Limiting unlensed source apparent magnitude")
    plotname = outdir+"Deg2AreaHistogramPlot"+timestamp+".pdf"
    ppt.savefig(plotname)

    cheat111()
    anglelims = [1e-3*CumulativeAreaNew[0], 1e-3*CumulativeAreaNew[-1], MvVals[0], MvVals[-1]]
    width = math.fabs(1e-3*CumulativeAreaNew[0] - 1e-3*CumulativeAreaNew[-1])
    height = math.fabs(MvVals[0] - MvVals[-1])
    aratio = aspectshrink*width/height
    ppt.imshow(numpy.log10(bHistCumulativeAreaNew), origin='lower', extent=anglelims, aspect=aratio)
    ppt.colorbar(shrink=cbshrink)
    conseq = [1e-3,1e-2,1e-1,1,10,1e2,1e3]
    ctrs = ppt.contour(bHistCumulativeAreaNew, conseq, origin='lower', extent=anglelims, aspect=aratio)
    ppt.clabel(ctrs)
    ppt.title("Logarithmic lower limit survey yield chart")
    ppt.xlabel("Angular area in 1000 deg$^2$ covered from galactic zenith")
    ppt.ylabel("Limiting unlensed source apparent magnitude")
    plotname = outdir+"Deg2AreaHistogramLogPlot"+timestamp+".pdf"
    ppt.savefig(plotname)

    cheat111()
    anglelims = [1e-3*CumulativeAreaNew[0], 1e-3*CumulativeAreaNew[-1], MvVals[0], MvVals[-1]]
    width = math.fabs(1e-3*CumulativeAreaNew[0] - 1e-3*CumulativeAreaNew[-1])
    height = math.fabs(MvVals[0] - MvVals[-1])
    aratio = aspectshrink2*width/height
    ppt.imshow(numpy.log10(bHistCumulativeAreaNew), origin='lower', extent=anglelims, aspect=aratio)
#    ppt.colorbar(shrink=cbshrink)
    conseq = [1e-3,1e-2,1e-1,1,10,1e2,1e3]
    ctrs = ppt.contour(bHistCumulativeAreaNew, conseq, origin='lower', extent=anglelims, aspect=aratio)
    ppt.clabel(ctrs)
    ppt.title("Lower limit survey yield chart")
    ppt.xlabel("Angular area in 1000 deg$^2$ covered from galactic zenith")
    ppt.ylabel("Limiting unlensed source apparent magnitude")
    plotname = outdir+"Deg2AreaHistogramLogDisplayPlot"+timestamp+".pdf"
    ppt.savefig(plotname)
